//
//  ZSNPicInPicViewController.swift
//  AgoraPicInPicZSN
//
//  Created by zhaosuning on 2022/1/25.
//

import UIKit
import AVKit

class ZSNPicInPicViewController: UIViewController , AVPictureInPictureControllerDelegate {
    var playerView: UIView!
    var picController: AVPictureInPictureController!
    var curPlayer: AVPlayer!
    
    var playerView2: UIView!
    var picController2: AVPictureInPictureController!
    var curPlayer2: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "画中画"
        self.view.backgroundColor = UIColor.white
        
        do {
            let session = AVAudioSession.sharedInstance()
            try session.setCategory(.playAndRecord)
            try session.setActive(true)
        } catch let error as NSError {
            print(error.localizedDescription)
            return
        }
        
//        self.playerView = UIView.init(frame: CGRect.init(x: 0, y: 100, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width*720.0/1280.0))
        
        self.playerView2 = UIView.init(frame: CGRect.init(x: 0, y: 450, width: 300, height: 300))
        self.playerView2.backgroundColor = UIColor.yellow
        self.view.addSubview(self.playerView2)
        
        
        
        self.playerView = UIView.init(frame: CGRect.init(x: 0, y: 100, width: 300, height: 300))
        self.playerView.backgroundColor = UIColor.yellow
        self.view.addSubview(self.playerView)
        
        let btnClick = UIButton.init(frame: CGRect(x: 30, y: 500, width: 200, height: 50))
        btnClick.setTitle("展示画中画", for: .normal)
        btnClick.backgroundColor = UIColor.red
        btnClick.addTarget(self, action: #selector(btnClickClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnClick)
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let videoUrls = [Bundle.main.url(forResource: "hahaha", withExtension: "MP4")!]
        
        let playerItem = AVPlayerItem(url: videoUrls[0])
        curPlayer = AVPlayer(playerItem: playerItem)
        let layer = AVPlayerLayer.init(player: curPlayer)
        layer.frame = self.playerView.bounds
        layer.backgroundColor = UIColor.red.cgColor
        self.playerView.layer.addSublayer(layer)
        
        self.picController = AVPictureInPictureController.init(playerLayer: layer)
        self.picController.delegate = self
        
        curPlayer.play()
        
        
        let playerItem2 = AVPlayerItem(url: videoUrls[0])
        curPlayer2 = AVPlayer(playerItem: playerItem2)
        let layer2 = AVPlayerLayer.init(player: curPlayer2)
        layer2.frame = self.playerView2.bounds
        layer2.backgroundColor = UIColor.red.cgColor
        self.playerView2.layer.addSublayer(layer2)
        
        
        self.picController2 = AVPictureInPictureController.init(playerLayer: layer2)
        self.picController2.delegate = self
        curPlayer2.play()
    }
    
    @objc func btnClickClick() {
        if self.picController.isPictureInPicturePossible {
            self.picController.startPictureInPicture()
        }
        else {
            
        }
        
        
        if self.picController2.isPictureInPicturePossible {
            self.picController2.startPictureInPicture()
        }
        else {
            
        }
    }
    
    
    func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
        
    }
    
    func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
        
    }
    
    func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
        
    }
    
    func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
        
    }
    
    func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {
        
    }
    
    func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
        
    }
    

}
